{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# To make horizontally vibrating pendulum rotate at constant anglular velocity = 1\n",
    "# the cartpole system is fully actuated, i.e rank(B) = dim(q), in other words, Mass Matrix is fully row rank\n",
    "import gym\n",
    "import gym_vibrating_pendulum\n",
    "from gym import wrappers\n",
    "\n",
    "\n",
    "import numpy as np\n",
    "import time\n",
    "from IPython.display import clear_output\n",
    "from matplotlib import pyplot as plt\n",
    "from matplotlib import animation as anime\n",
    "env = gym.make('VibratingPendulum-v0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = 1.\n",
    "l = 0.5\n",
    "w = 1.\n",
    "h = 1.\n",
    "g = 9.8\n",
    "done = False\n",
    "t = 0\n",
    "\n",
    "# env = gym.wrappers.Monitor(env, \"vid\")\n",
    "s = env.reset()\n",
    "\n",
    "while not done:\n",
    "\n",
    "    u = (m*g*l*np.sin(s[2]))-(m*(w**2)*h*l*np.sin(w*t)*np.cos(s[2]))\n",
    "\n",
    "    sm = 0\n",
    "\n",
    "    action = np.array([t, u, sm])\n",
    "\n",
    "    news, rew, done, _ = env.step(action)\n",
    "    sm += (news[3] - s[3])\n",
    "    s = news \n",
    "    env.render()\n",
    "    t += 0.005\n",
    "\n",
    "    if abs(1-s[3]) <= 1e-10:\n",
    "        done = True\n",
    "\n",
    "done = True\n",
    "env.close()    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "goal error 9.905298803403184e-11\n"
     ]
    }
   ],
   "source": [
    "print(\"goal error\", 1 - s[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
